#!/bin/sh

# launch-bristol.sh - by B. Watson <urchlay@slackware.uk>, licensed
# under the WTFPL. Part of the SlackBuilds.org bristol build.

# Simple KDialog-based launcher for Bristol synth. Bristol is a
# GUI application once it's running, but it emulates 40+ different
# synths, and the only way to choose the synth is by passing an
# argument to startBristol (e.g. -mini or -prophet).

# We want to be able to launch it from a .desktop file, which means
# one of three things:

# 1. The .desktop file would have a synth type hardcoded, other emulations
#    would require CLI startup.
# 2. There would have to be 40+ .desktop files, one per synth type (ugh).
# 3. The .desktop file would launch some kind of selector to let the user
#    pick the synth type. That's what this script is for.

# This works, but doesn't offer a way to set any of the other CLI
# options. We have "-jack" hardcoded here, since I doubt many people
# ever use "-alsa" with bristol...

# If this file exists, it contains the last choice the user made,
# last time this script was run and its OK button pressed.
# Pressing Cancel doesn't create or alter this file.
file=~/.launch-bristol

# This is more complex than it should be, because kdialog's --default
# option requires the *text* of the selected item (e.g. 'moog mini'),
# but kdialog doesn't print this text (it prints the ID of the option,
# e.g. '-mini'). So this script greps itself to find the item text
# that goes with the saved item ID.
if [ -e $file ]; then
  dflt="$( cat $file )"
  dflttxt="$( grep "^  $dflt" $0 | cut -d"'" -f2 )"
else
  dflttxt='hammond B3 (default)'
fi

# Try to center the window. Unfortunately kdialog's not smart enough
# to auto-size, I have to hardcode a width and height to avoid having
# a tiny window that requires a ton of scrolling. Also I have no idea
# how (or if) the font and font-size are chosen...
xpos=0; ypos=0
width=400; height=800
x="$( xwininfo -root | grep Width | sed 's,.* ,,' )"
y="$( xwininfo -root | grep Height | sed 's,.* ,,' )"
if [ "$x" -gt "$width" -a "$y" -gt "$height" ]; then
  xpos=$(( $x / 2 - $width / 2 ))
  ypos=$(( $y / 2 - $height / 2 ))
fi

# The list of synths came from "startBristol --help".
kdialog --title 'Bristol Launcher' --menu 'Select Synth Emulation' \
  --geometry ${width}x${height}+${xpos}+${ypos} \
  --default "$dflttxt" -- \
  -b3 'hammond B3 (default)' \
  -mini 'moog mini' \
  -explorer 'moog voyager' \
  -voyager 'moog voyager electric blue' \
  -memory 'moog memory' \
  -sonic6 'moog sonic 6' \
  -mg1 'moog/realistic mg-1 concertmate' \
  -hammond 'hammond module (deprecated, use -b3)' \
  -prophet 'sequential circuits prophet-5' \
  -pro52 'sequential circuits prophet-5/fx' \
  -pro10 'sequential circuits prophet-10' \
  -pro1 'sequential circuits pro-one' \
  -rhodes 'fender rhodes mark-I stage 73' \
  -rhodesbass 'fender rhodes bass piano' \
  -roadrunner 'crumar roadrunner electric piano' \
  -bitone 'crumar bit 01' \
  -bit99 'crumar bit 99' \
  -bit100 'crumar bit + mods' \
  -stratus 'crumar stratus synth/organ combo' \
  -trilogy 'crumar trilogy synth/organ/string combo' \
  -obx 'oberheim OB-X' \
  -obxa 'oberheim OB-Xa' \
  -axxe 'arp axxe' \
  -odyssey 'arp odyssey' \
  -arp2600 'arp 2600' \
  -solina 'arp/solina string ensemble' \
  -polysix 'korg polysix' \
  -poly800 'korg poly-800' \
  -monopoly 'korg mono/poly' \
  -ms20 'korg ms20 (unfinished: -libtest only)' \
  -vox 'vox continental' \
  -voxM2 'vox continental super/300/II' \
  -juno 'roland juno-60' \
  -jupiter 'roland jupiter-8' \
  -bme700 'baumann bme-700' \
  -bm 'bristol bassmaker sequencer' \
  -dx 'yamaha dx-7' \
  -cs80 'yamaha cs-80 (unfinished)' \
  -sidney 'commodore-64 SID chip synth' \
  -melbourne 'commodore-64 SID polysynth (unfinished)' \
  -granular 'granular synthesiser (unfinished)' \
  -aks 'ems synthi-a (unfinished)' \
  -mixer '16 track mixer (unfinished: -libtest only)' \
  > $file.new

opt="$( cat $file.new )"
if [ "$opt" = "" ]; then
  rm -f $file.new
  exit 0
fi

mv $file.new $file
if [ "$1" = "--fake" ]; then
  echo "exec startBristol -jack $opt"
else
  exec startBristol -jack $opt
fi
